/*
 *  linux/kernel/who.c
 *
 *  July 27, 2010; July 28, 2010
 *  clzqwdy@gmail.com
 */
#include <linux/kernel.h>	/* printk */
#include <asm/segment.h>	/* get_fs_XXX, put_fs_XXX */
#include <errno.h>			/* EINVAL */

#define MAX_NAME_LEN        23

char myname[MAX_NAME_LEN+1];	/* global varible */

int sys_iam(const char* name)
{
	unsigned char c;
	int len, i;
	char temp[MAX_NAME_LEN+1];

//	printk("it is in sys_iam\n");	/* or testlab2.sh will fail */
	len = 0;

	for ( i = 0; i <= MAX_NAME_LEN; i++ ) {
		if ( ( c = get_fs_byte( &name[i] ) ) != '\0' ) {
			++len;
			temp[i] = c;
		} else {
			break;	
		}
	}

	if (c != '\0') {
		return -EINVAL;	
	}	

	for ( i = 0; i < len; i++ ) {
		myname[i] = temp[i];
	}
	myname[i] = '\0';

	return (len);	
}

int sys_whoami(char* name, unsigned int size)
{
	int len, i;

//	printk("it is in sys_whoami\n");
	len = 0;

	for (i = 0; i <= MAX_NAME_LEN; i++) {
		if (myname[i] != '\0') {
			++len;	
		} else {
			break;
		}
	}

	if (size < len) {
		return -EINVAL;
	}

	for (i = 0; i < len; i++) {
		put_fs_byte(myname[i], &name[i]);		
	}
	name[i] = '\0';
	return (len);	
}








